package gui;
/**
 * @author Adam
 * this Class presents the Gui of the option "Set Execution duration" ,  it extends from the Class "SetTaksDuration"
 */
import javax.swing.JPanel;

import Entities.RequestEntity;
import common.ChatIF;
import javax.swing.JCheckBoxMenuItem;
import java.awt.Choice;
import java.awt.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;


public class SetExecutionDuration extends SetTaskDuration implements ChatIF{
	JComboBox<String> ExRequestsComboBox;
	private JTextField daysTextField;
	private JButton btnSubmit;
	private String newStatus;
	private ViewRequestPanel viewRequest;
	private JButton btnViewRequest;
	private SetExecutionDuration myself;
	private String requestNum;
	public static JComboBox<String> comboBox;
	
	
	public SetExecutionDuration (AcademicFrame frame,ExecutionLeaderPanel eleader,String pid){
		super(frame,eleader,pid);
		this.myself = this;
		initialize();
	
	}
public void initialize(){
	daysTextField = new JTextField();
	daysTextField.setBounds(271, 170, 86, 20);
	add(daysTextField);
	daysTextField.setColumns(10);
	
	btnSubmit = new JButton("Set duration");
	btnSubmit.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			newStatus="9";
			String request=ExRequestsComboBox.getSelectedItem().toString();	
			String days= daysTextField.getText();
			message= new ArrayList<Object>();
			message.add("SetTaskDuration");
			message.add(request);
			message.add(days);
			message.add(newStatus);
		
			AcademicFrame.myclient.sendMessageToServer(message);
			
		}
		
	});
	JButton btnViewRequest = new JButton("View request");
	btnViewRequest.addActionListener(new ActionListener() 
	{
		public void actionPerformed(ActionEvent e)
		{
			if (requestNum.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
				(new Dialog("Please choose a request from the list first.")).setVisible(true);
			else{
			viewRequest = new ViewRequestPanel(requestNum,frame,myself,false,"");
			frame.setSize(550, 730);
			frame.setContentPane(viewRequest);
			}
			
		}
	});
	btnViewRequest.setBounds(158, 253, 130, 23);
	add(btnViewRequest);
	btnSubmit.setBounds(298, 253, 130, 23);
	add(btnSubmit);
	ExRequestsComboBox = new JComboBox<String>();
	ExRequestsComboBox.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			JComboBox cb = (JComboBox)e.getSource();
			requestNum = (String)cb.getSelectedItem();
		}
	});
	
	ExRequestsComboBox.setBounds(87, 112, 130, 20);
	add(ExRequestsComboBox);
	
}
	@Override
	public void display(Object message)
	{
		ArrayList<Object> listofExRequests=new ArrayList<Object>();
		ArrayList<Object> TaskDuration=new ArrayList<Object>();
		listofExRequests.clear(); // clear the info array list
		TaskDuration.clear(); // clear the info array list
		listofExRequests = (ArrayList<Object>)message;
		TaskDuration = (ArrayList<Object>)message;
		
	
		if (((String)(listofExRequests.get(0))).equalsIgnoreCase("listOfExRequests"))
		{
			ArrayList<String> assessorRequests = (ArrayList<String>)message; 
									
			int numberOfSystems = Integer.parseInt((assessorRequests.get(1)).toString()); // get the number of systems in the list
			this.ExRequestsComboBox.addItem("--Choose a Request--");
			for (int i=0; i < numberOfSystems ; i++ )
			{
				this.ExRequestsComboBox.addItem((String)assessorRequests.get(i+2));  // add systems to the list
			}
		}	
		else if (((String)(TaskDuration.get(0))).equalsIgnoreCase("TaskDuration"))
		{
			
			ArrayList<String> replyFromServer = (ArrayList<String>)message; 
										
			if ( ((String)(replyFromServer.get(1))).equalsIgnoreCase("TaskDurationSet") )
			{
				
				(new Dialog ("The assessment duration was set, thank you.")).setVisible(true);
				
				frame.setSize(500, 500);
				frame.setContentPane(panel);	
			}
			else
			{
				(new Dialog ("An Error occured while writing to the database.")).setVisible(true);
				
				frame.setSize(500, 500);
				frame.setContentPane(panel);	
			}
		}			
	}
}
